package com.moon.game.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.moon.game.constants.Constants;
import com.moon.game.manager.GiftMG;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Gift;
import com.moon.game.util.Utils;
import com.moon.game.vos.GiftVo;
import com.moon.game.vos.PlayerVo;

/**
 * @description 礼包服务
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-8-7 下午2:48:59
 */
public class GiftService {
	
	private static final Logger log = Logger.getLogger(GiftService.class);
	
	/**
	 * 删除礼包
	 * 
	 * @param sid
	 * @param pid
	 * @param giftId
	 * @param num添加的礼包数量
	 * @return-1没有找到玩家,0添加成功
	 */
	public static int addGift(String sid, String pid, int giftId, int num) {
		PlayerVo player = PlayerService.doGetPlayer(sid, pid);
		if (player == null) {
			return -1;
		}
		List<GiftVo> gifts = player.getGiftList();
		if (gifts == null) {
			gifts = new ArrayList<GiftVo>();
		}
		
		List<GiftVo> newGifts = new ArrayList<GiftVo>();
		if (checkGift(player, giftId)) {
			for (GiftVo vo : gifts) {
				if (vo.getGiftId() == giftId) {
					vo.setGiftNum(vo.getGiftNum() + num);
				}
				newGifts.add(vo);
			}
		}else {
			newGifts.addAll(gifts);
			GiftVo vo = new GiftVo();
			vo.setGiftId(giftId);
			vo.setGiftNum(num);
			newGifts.add(vo);
		}
		player.setGiftList(newGifts);
		PlayerService.doUpdatePlayer(sid, pid, player);
		return 0;
	}
	
	
	/**
	 * 删除礼包
	 * 
	 * @param player
	 * @param giftId
	 * @return -1缺少礼包不能删除,0删除成功,-2获取人物失败
	 */
	public static int delGift(String sid, String pid, int giftId) {
		PlayerVo player = PlayerService.doGetPlayer(sid, pid);
		if (player == null) {
			return -2;
		}
		if (checkGift(player, giftId)) {
			List<GiftVo> gifts = player.getGiftList();
			List<GiftVo> newGifts = new ArrayList<GiftVo>();
			for (GiftVo vo : gifts) {
				if (vo.getGiftId() == giftId) {
					vo.setGiftNum(vo.getGiftNum() - 1);
					if (vo.getGiftNum() <= 0) {
						continue;
					}
				}
				newGifts.add(vo);
			}
			player.setGiftList(newGifts);
			return 0;
		}else {
			return -1;
		}
	}
	
	
	/**
	 * 检查指定玩家是否有指定礼包
	 * 
	 * @param player
	 * @param giftId
	 * @return true 有，false没有
	 */
	public static boolean checkGift(PlayerVo player, int giftId) {
		List<GiftVo> gifts = player.getGiftList();
		if (gifts == null) {
			return false;
		}
		//查找是否有指定的礼包
		boolean hasTheGift = false;
		for (GiftVo vo : gifts) {
			if (vo.getGiftId() == giftId) {
				hasTheGift = true;
				break;
			}
		}
		return hasTheGift;
	}
	
	
	/**
	 * 给玩家添加打开礼包的道具
	 * 
	 * @param player
	 * @param giftId
	 */
	public static void addGiftReward(PlayerVo player, int giftId) {
		Gift gift = GiftMG.instance.getGiftById(giftId);
		if (gift.getExp() != null && gift.getExp() > 0) {
			PlayerService.doAddExp(player.getSid(), player.getId(), gift.getExp());
		}
		if (gift.getGold() != null && gift.getGold() > 0) {
			PlayerService.doAddGold(player, gift.getGold());
		}
		if (gift.getMoney() != null && gift.getMoney() > 0) {
			PlayerService.doAddMoney(player, gift.getMoney());
		}
		if (analysisGiftItem(giftId) != null) {
			HashMap<Integer, Integer> allRewardItems = analysisGiftItem(giftId);
			HashMap<Integer, Integer> items = new HashMap<Integer, Integer>();
			for (Integer id : allRewardItems.keySet()) {
				if (ItemMG.instance.getItem(id) == null) {
					//如果不是道具，则认为是礼包
					addGift(player.getSid(), player.getId(), id, allRewardItems.get(id));
					continue;
				}
				items.put(id, allRewardItems.get(id));
			}
			int ret = ItemService.addItems(player, new ArrayList<Integer>(items.keySet()), new ArrayList<Integer>(items.values()), true);
			if (ret == Constants.ADD_ITEM_NO_ROOM) {
				log.debug(player.getId() + "fail open gift:" + giftId + "|noroom");
			}
			if (ret == Constants.ADD_ITEM_FAIL) {
				log.debug(player.getId() + "fail open gift:" + giftId + "|exception");
			}
		}
	}
	
	
	/**
	 * 解析礼包包含道具
	 * 
	 * @param giftId
	 * @return
	 */
	public static HashMap<Integer, Integer> analysisGiftItem(int giftId) {
		Gift gift = GiftMG.instance.getGiftById(giftId);
		String itemReward = gift.getItemNum();
		if (itemReward == null || itemReward.length() == 0) {
			return null;
		}
		return Utils.analyzeString(itemReward);
	}
}
